package com.summer_administration.controller;

import com.summer_administration.pojo.Member;
import com.summer_administration.pojo.PageBean;
import com.summer_administration.pojo.Result;
import com.summer_administration.service.MemberService;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/members")
@Validated
public class MemberController {
    @Autowired
    private MemberService memberService;

    @PostMapping
    public Result add(@RequestBody @Validated Member member) {
        memberService.add(member);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Member>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        PageBean<Member> pb = memberService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated Member member) {
        memberService.update(member);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        memberService.delete(id);
        return Result.success();
    }

    @GetMapping("/getDetailsByMemberName")
    public Result<List<Member>> getDetailsByMemberName(
            @RequestParam @NotBlank(message = "成员名不能为空") String memberName) {
        List<Member> members = memberService.getDetailsByMemberName(memberName);
        return Result.success(members);
    }
}